package com.zjinyin.forum.controller;

import com.sun.istack.internal.NotNull;
import com.zjinyin.forum.common.AppResult;
import com.zjinyin.forum.common.ResultCode;
import com.zjinyin.forum.config.AppConfig;
import com.zjinyin.forum.model.Article;
import com.zjinyin.forum.model.ArticleReply;
import com.zjinyin.forum.model.User;
import com.zjinyin.forum.service.IArticleReplyService;
import com.zjinyin.forum.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2023-08-11
 * Time: 19:46
 */

@Api(tags = "回复帖子接口")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Resource
    private IArticleService articleService;

    @Resource
    private IArticleReplyService articleReplyService;


    /**
     * 根据主贴回复一个帖子
     *
     * @param articleId 主贴 id
     * @param content   回复内容
     */
    @ApiOperation("创建回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("回复帖子的 Id") @RequestParam("articleId") @NotNull Long articleId,
                            @ApiParam("帖子正文") @RequestParam("content") @NotNull String content) {

        // 提前检验 ArticleId
        if (articleId <= 0) {
            log.warn("ArticleId : " + articleId + " " + ResultCode.FAILED_ARTICLE_NOT_EXISTS);
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        // 1. 获取当前登陆用户信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        // 2. 检验用户状态
        if (user.getState() == 1) {
            log.warn("userId : " + user.getId() + " " + ResultCode.FAILED_USER_BANNED);
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        // 3. 查看当前要回复的帖子状态
        Article article = articleService.selectById(articleId);
        if (article == null || article.getState() == 1) {
            log.warn("ArticleId : " + articleId + " " + ResultCode.FAILED_ARTICLE_ABNORMAL);
            return AppResult.failed(ResultCode.FAILED_ARTICLE_ABNORMAL);
        }

        // 帖子已删除
        if (article.getDeleteState() == 1) {
            log.warn("ArticleId : " + articleId + " " + ResultCode.FAILED_ARTICLE_NOT_EXISTS);
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }

        // 4. 创建回复帖子
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId); // 主贴 id - 给那个帖子回复
        articleReply.setPostUserId(user.getId()); // 发送者 id - 当前登录用户
        articleReply.setContent(content); // 回复的帖子内容

        // 5. 写回数据库
        articleReplyService.create(articleReply);
        return AppResult.success();
    }


    /**
     * 根据主贴 id 获取该帖子下的所有回复帖子
     *
     * @param articleId 主贴 id
     */
    @ApiOperation("根据 Id 获取帖子下的所有回复")
    @GetMapping("/getReplyArticleById")
    public AppResult getReplyArticleById(@ApiParam("主贴 id") @RequestParam("articleId") @NotNull Long articleId) {

        // 2. 根据articleId 查询是否存在该帖子
        Article article = articleService.selectById(articleId);

        if (article == null) {
            log.warn("主贴 ArticleId : " + articleId + " " + ResultCode.FAILED_NOT_EXISTS);
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }

        if (article.getState() == 1) {
            log.warn("主贴 ArticleId : " + articleId + " " + ResultCode.FAILED_ARTICLE_ABNORMAL);
            return AppResult.failed(ResultCode.FAILED_ARTICLE_ABNORMAL);
        }

        if (article.getDeleteState() == 1) {
            log.warn("主贴 ArticleId : " + articleId + " " + ResultCode.FAILED_ARTICLE_NOT_EXISTS);
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }

        // 3. 查询帖子列表
        List<ArticleReply> list = articleReplyService.selectReplyArticleById(articleId);

        return AppResult.success(list);
    }
}
